package com.idrive.activity;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;

import com.idrive.R;
import com.idrive.service.IDriveService;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final ToggleButton serviceButton = (ToggleButton) findViewById(R.id.serviceButton);

		if (isIDriveRunning()) {
			serviceButton.toggle();
		}

		serviceButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				if (isIDriveRunning()) {
					stopService(new Intent(MainActivity.this, IDriveService.class));
				} else {
					startService(new Intent(MainActivity.this, IDriveService.class));
				}
			}

		});

		Button callManagerButton = (Button) findViewById(R.id.callManagerButton);
		callManagerButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent myIntent = new Intent(MainActivity.this, CallManagerActivity.class);

				MainActivity.this.startActivity(myIntent);
			}

		});

		Button smsReaderButton = (Button) findViewById(R.id.smsReaderButton);
		smsReaderButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent myIntent = new Intent(MainActivity.this, SMSReaderActivity.class);

				MainActivity.this.startActivity(myIntent);
			}

		});

		Button pathTrackerButton = (Button) findViewById(R.id.pathTrackerButton);
		pathTrackerButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent myIntent = new Intent(MainActivity.this, PathTrackerActivity.class);

				MainActivity.this.startActivity(myIntent);
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private boolean isIDriveRunning() {
		ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
			if ("com.idrive.service.IDriveService".equals(service.service.getClassName())) {
				return Boolean.TRUE;
			}
		}
		return Boolean.FALSE;
	}

}
